/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

/**
 * <p>Project: javaseapp - Ex6
 * <p>Powered by webrx On 2021-08-25 15:30:25
 * <p>功能：3个吃饭，到起开饭
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class Ex6 {
    CyclicBarrier barrier = new CyclicBarrier(3, () -> System.out.println("大家到齐，现在开宴!!!"));

    void eat() {
        String tn = Thread.currentThread().getName();
        Random rand = new Random();
        int time = rand.nextInt(30) + 15;
        System.out.printf("%s 动身(时间：%d秒)...%n", tn, time);
        try {
            TimeUnit.SECONDS.sleep(time);
            System.out.printf("%s到达%n", tn);
            barrier.await();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        var t = new Ex6();
        new Thread(t::eat, "李四").start();
        new Thread(() -> t.eat(), "王五").start();
        new Thread(t::eat, "王五").start();
        new Thread(t::eat, "赵六").start();
        new Thread(t::eat, "王五").start();
        new Thread(t::eat, "张三丰").start();
    }
}
